package database;

/**
 * Created by perrotti.f on 1/04/2014.
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("comments")
public class CommentController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private CommentRepository comments;

    @Autowired
    private EntryRepository entries;

    @RequestMapping("test")
    public String test() {
        log.info("Test");
        return "OK";
    }


    @RequestMapping("/{id_entry}")
    @ResponseBody
    public List<Comment> getComments(@PathVariable("id_entry") long idEntry) {
        log.info("Get comments of entry:"+idEntry);
        Entry e = entries.findOne(idEntry);
        return e.getComments();
    }

    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    public void addComment(@RequestBody Comment comment) {
        log.info("Adding comment to entry");
        log.info(comment.toString());
        comments.save(comment);
    }

    @RequestMapping(value = "/delete/{id_comment}",method = RequestMethod.DELETE)
    public void deleteComment(@PathVariable("id_comment") long idEntry) {
        log.info("Erasing comment id:"+idEntry);
        comments.delete(idEntry);
    }

    @RequestMapping("")
    @ResponseBody
    public List<Comment> listAll() {
       return comments.findAll();
    }





}
